package com.cya.lotterysystem.service.impl;

import com.cya.lotterysystem.common.errcode.ServiceErrorCodeConstans;
import com.cya.lotterysystem.common.exception.ServiceException;
import com.cya.lotterysystem.service.PictureService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @author ：陈奕安（3048279304@qq.com）
 * @date ：Created in 2025/11/1 20:13
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class PictureServiceImpl implements PictureService {

    @Value("${pic.local-path}")
    private String localPath;

    @Override
    public String savePicture(MultipartFile multipartFile) {



//        创建目录
        File dir=new File(localPath);
        if (!dir.exists()){
            dir.mkdirs();
        }
//        创建索引
//        aaa。jpg->xxx.jpg

        String filename = multipartFile.getOriginalFilename();

        assert filename!=null;

        String suffix=filename.substring(
                filename.lastIndexOf("."));
        filename= UUID.randomUUID()+suffix;
//        图片保存服务
        try {
            multipartFile.transferTo(new File(localPath+"/"+filename));
        } catch (IOException e) {
            throw new ServiceException(ServiceErrorCodeConstans.PIC_UPLOAD_ERROR);
        }

        return filename;
 }
}